package com.example.smartcity.ui.SmartCommynity;

import androidx.annotation.LongDef;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.CommandListAdapter;
import com.example.smartcity.ui.Adapter.RealtyManagementRecAdapter;
import com.example.smartcity.ui.Bean.CommandBean;
import com.example.smartcity.ui.Bean.RealityManagementBean;
import com.google.android.material.bottomsheet.BottomSheetBehavior;

import java.util.ArrayList;
import java.util.Calendar;

public class RealtyManagement extends AppCompatActivity {

    private RecyclerView RV_RealtyManage;
    private ArrayList<RealityManagementBean> list = new ArrayList<>();
    private String[] Permissions = new String[]{
            Manifest.permission.CALL_PHONE
    };
    private static ArrayList<CommandBean> CommandList = new ArrayList();
    private Calendar calendar = Calendar.getInstance();
    private final int REQUESTCODE = 1;
    private RecyclerView LVCommend;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_realty_management);
        RV_RealtyManage = findViewById(R.id.RV_RealtyManage);
        initView();
    }
    public void initView(){
        //申请权限
        getPermissions();
        getPermissions();
        loadMessage();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.VERTICAL);
        RV_RealtyManage.setLayoutManager(manager);
        RealtyManagementRecAdapter adapter = new RealtyManagementRecAdapter(list,R.layout.realitymanagerment);
        adapter.SetOnItemClickListener(new RealtyManagementRecAdapter.OnItemClickListener() {
            @Override
            public void OnRCVItemClickListener(int position,View view) {
                //显示底部的输入栏
                BottomSheetBehavior behavior = BottomSheetBehavior.from(findViewById(R.id.Bottom_Commend));
                if (behavior.getState()==BottomSheetBehavior.STATE_EXPANDED){
                    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }else{
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
                //评论
                behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                    @Override
                    public void onStateChanged(@NonNull View bottomSheet, int newState) {
                        LVCommend = view.findViewById(R.id.LV_commend);
                        EditText editText = bottomSheet.findViewById(R.id.ET_Command);
                        Button button = bottomSheet.findViewById(R.id.btn_sunmit);
                        button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                CommandBean bean = new CommandBean(editText.getText().toString(),calendar.get(Calendar.YEAR)+"年"+calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日"+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
                                CommandList.add(bean);
                                loadCommand(LVCommend);
                                behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                            }
                        });
                    }

                    @Override
                    public void onSlide(@NonNull View bottomSheet, float slideOffset) {

                    }
                });
            }
        });
        adapter.SetOnRCVItemClickListenerForCallPhone(new RealtyManagementRecAdapter.OnItemClickListenerForCallPhone() {
            @Override
            public void OnRCVItemClickListenerForCallPhone(int position, View view) {
                TextView textView = view.findViewById(R.id.TV_ManagerPhone);
                Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+textView.getText().toString()));
                startActivity(intent);
            }
        });
        RV_RealtyManage.setAdapter(adapter);
    }
    //加载评论列表
    public void loadCommand(RecyclerView LVCommend){
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.VERTICAL);
        LVCommend.setLayoutManager(manager);
        LVCommend.setAdapter(new CommandListAdapter(R.layout.commanditemlayout,CommandList));
    }

    //加载数据
    public void loadMessage(){
        RealityManagementBean b1 = new RealityManagementBean(R.drawable.social1,"物业服务","南京悠家公寓物业服务竭诚欢迎您的到来","12312656325");
        RealityManagementBean b2 = new RealityManagementBean(R.drawable.social2,"停车位服务中心","海德停车场欢迎您的到来","1235");
        RealityManagementBean b3 = new RealityManagementBean(R.drawable.social3,"便民服务中心","便民服务中心欢迎您的到来","1234");
        RealityManagementBean b4 = new RealityManagementBean(R.drawable.social4,"保修服务","24小时服务欢迎您的到来","9999999999");
        list.add(b1);
        list.add(b2);
        list.add(b3);
        list.add(b4);
    }

    //获取权限
    public void getPermissions(){
        for (int i = 0; i < Permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(this,Permissions[i])!= PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(RealtyManagement.this,Permissions,REQUESTCODE);
            }
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case REQUESTCODE:
                for (int i = 0; i < grantResults.length; i++) {
                    if (grantResults.length>0 && grantResults[i]==PackageManager.PERMISSION_GRANTED){
                        Toast.makeText(this, "权限申请成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(this, "权限申请失败", Toast.LENGTH_SHORT).show();
                        this.finish();
                    }
                }
                break;
        }
    }


}